package cn.wy.basic.util;

import cn.wy.basic.jwt.JwtUtils;
import cn.wy.basic.jwt.LoginData;
import cn.wy.basic.jwt.Payload;
import cn.wy.basic.jwt.RsaUtils;
import cn.wy.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

public class LoginContext {
    public static Logininfo getLoginInfo(HttpServletRequest request){
        try {
            //获取请求头token
            String token = request.getHeader("token");
            if (token != null) {
                //获取公钥
                PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("auth_rsa.pub").getFile());
                //解密
                Payload<LoginData> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginData.class);
                Logininfo logininfo = payload.getLoginData().getLogininfo();
                return logininfo;
            } } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
